/*
 * PuyoIO.java
 *
 * Created on 2006.12.13, PM 9:45
 *
 * Handle record store IO.
 */

package Puyo;
import javax.microedition.rms.*;
/**
 *
 * @author Ming Li
 */
public class PuyoIO {
    
    /** Creates a new instance of PuyoIO */
    public PuyoIO() {
    }
    
    /** Overwrite the content of a store with a byte array */
    public static void write(String storeName, byte[] data){
      try {
        RecordStore.deleteRecordStore(storeName);
      }
      catch (RecordStoreException ex) {}
      // write file
      try {
       RecordStore rec = RecordStore.openRecordStore(storeName, true);
       rec.addRecord( data, 0, data.length );
       rec.closeRecordStore();
      }
      catch (RecordStoreException ex) {
       System.out.println(ex.toString());
      }
    }
    
    /** Read the content of a store to a byte array */
    public static byte[] read(String storeName){
      byte[] data = null;
      try {
        RecordStore rec = RecordStore.openRecordStore(storeName, false);
        RecordEnumeration re = rec.enumerateRecords(null, null, false);
        if (re.numRecords() > 0)
          data = re.nextRecord();
        rec.closeRecordStore();
      }
      catch (RecordStoreException ex) {
        return null;
      }
      return data;
    }
}
